﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Timers;
using System.Windows.Forms;

namespace TimerTool
{
    public partial class Form1 : Form
    {
        private readonly System.Timers.Timer timer = new System.Timers.Timer();

        private string importMsg { get; set; }
		
        public Form1()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (timer.Enabled)
            {
                MessageBox.Show("您已启动成功，无需重复点击！");
                return;
            }
            int it = 0;
            string mytime = this.mytime.Text;
            string myMsg = this.input.Text;
            if (string.IsNullOrEmpty(myMsg))
            {
                MessageBox.Show("请您输入的要提醒的事情");
                return;
            }
            if (!this.jtsj.Checked)
            {
                if (string.IsNullOrEmpty(mytime))
                {
                    MessageBox.Show("请您输入的多久提醒一次");
                    return;
                }

                try
                {
                    it = int.Parse(mytime);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    MessageBox.Show("您输入的间隔时间格式不合法，请输入整数，如：想在1小时后提醒请输入1");
                    return;
                }
            }
            Console.WriteLine($"您输入的是{mytime}");
            this.importMsg = myMsg;
            if (this.jtsj.Checked)
            {
                if (string.IsNullOrEmpty(this.meslabel.Value.ToString()))
                {
                    MessageBox.Show("请您输入具体时间");
                    return;
                }
                DateTime dt1 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                DateTime dt2 = DateTime.Parse(this.meslabel.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                //以下函数只能粗略判断日期前后
                if (DateTime.Compare(dt2, dt1) <= 0) //大于返回1，小于返回-1，等于返回0
                {
                    MessageBox.Show("提醒时间不能小于当前时间");
                    return;
                }
                //利用TimeSpan计算时间差
                TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                TimeSpan ts3 = ts2.Subtract(ts1); //ts2-ts1
                long sumSeconds = long.Parse(ts3.TotalSeconds.ToString()); //得到相差秒数
                MessageBox.Show($"具体提醒时间，将在={sumSeconds}秒后进行");
                if (sumSeconds > 24 * 60 * 60) //判断是否大于一天
                {
                    MessageBox.Show("提醒时间要设置到一天后嘛？如果是，请继续");
                }
                it = int.Parse(sumSeconds.ToString());
            }


            if (this.ss.Checked)
            {
                timer.Interval = it * 1000;
                timer.Elapsed += new ElapsedEventHandler(timer1_Tick);
                timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                timer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            }
            else if (this.mm.Checked)
            {
                timer.Interval = it * 1000 * 60;
                timer.Elapsed += new ElapsedEventHandler(timer1_Tick);
                timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                timer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            }
            else
            {
                timer.Interval = it * 1000;
                timer.Elapsed += new ElapsedEventHandler(timer1_Tick);
                timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                timer.AutoReset = false;//设置是执行一次（false）还是一直执行(true)；
            }
            timer.Start();
            MessageBox.Show("启动成功......");
            this.button1.BackColor = Color.Green;


            string filePath = Application.StartupPath + "//" + DateTime.Now.ToString("yyyy-MM-dd") + "importsomething.txt";
            bool isE = File.Exists(filePath);
            if (isE)
            {
                myMsg += Environment.NewLine + read(filePath) + Environment.NewLine;
            }
            string DamyMsg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":【" + myMsg + "】";
            StreamWriter m_streamWriter = new StreamWriter(filePath, false, Encoding.Default);
            m_streamWriter.Flush();
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);//0标识指针，SeekOrigin.Begin 标识开始位置
            m_streamWriter.Write(DamyMsg);
            m_streamWriter.Flush();
            m_streamWriter.Close();
            // 隐藏窗体 
            this.Hide();

        }

        private string read(string path)
        {
            StreamReader read = new StreamReader(path, Encoding.Default);
            string str = string.Empty;
            while (read.Peek() != -1)
            {
                str += read.ReadLine() + Environment.NewLine;
                Console.WriteLine(str);
            }
            read.Close();
            return str;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime time = DateTime.Now;
            string now = time.ToString("yyyy-MM-dd hh:mm:ss");
            Form2 dialog = new Form2(this.importMsg, $"当前时间:{ now }");//new 一个通知  
            dialog.ShowDialog();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.input.Text = @"重要的事情说三遍：
                                下班不要忘记打卡哦！！！
                                下班不要忘记打卡哦！！！
                                下班不要忘记打卡哦！！！";
            this.mm.Checked = true;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.meslabel.Value.ToString("yyyy-MM-dd HH:mm:ss");

        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            this.label4.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = Application.StartupPath + "//" + DateTime.Now.ToString("yyyy-MM-dd") + "importsomething.txt";
            bool isE = File.Exists(filePath);
            if (!isE)
            {
                MessageBox.Show("暂无历史记录......");
                return;
            }
            System.Diagnostics.Process.Start(filePath);

        }

        private void Stop_Click(object sender, EventArgs e)
        {
            MessageBox.Show("停止任务成功......");
            timer.Stop();
            timer.Enabled = false;
            timer.Dispose();
            this.button1.BackColor = Color.Red;
        }
    }
}
